package com.pactera.asmp.server.common.utils;

import com.pactera.asmp.server.common.constants.Result;
import com.pactera.asmp.server.common.constants.ResultCode;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.http.HttpServletResponse;

/**
 * @ProjectName: asmpserver
 * @Package: com.pactera.asmp.server.common.utils
 * @ClassName: FileUtils
 * @Author: lvzhiqiang
 * @Description:
 * @Date: 2019/8/27 16:38
 * @Version: 1.0
 */
public class FileUtils {
    private static final Logger logger = LoggerFactory
            .getLogger(FileUtils.class);

    private FileUtils(){}
    /**
     * @Method saveIssueTraceFile
     * @Description saveIssueTraceFile
     * @param: [file, issue]
     * @return: boolean
     * @auther: lzq
     * @date: 2019/8/27 16:43
     */
    public static boolean saveIssueTraceFile(String path, MultipartFile file){
        File traceFile = new File(path);
        if(!traceFile.getParentFile().exists()){
            traceFile.getParentFile().mkdirs();
        }
        try {
            file.transferTo(traceFile); //保存文件
            return true;
        } catch (IllegalStateException | IOException e) {
            logger.info(e.getMessage());
            return false;
        }
    }

    /**
     * @Method dowloadFile
     * @Description dowloadFile
     * @param: [response, tracePath]
     * @return: com.pactera.asmp.server.utils.Result
     * @auther: lzq
     * @date: 2019/8/28 18:13
     */
    public static Result dowloadFile(HttpServletResponse response,String tracePath){
        Result<String> result = new Result(ResultCode.SUCCESS, "Trace download sucess");
        String fileName = tracePath.substring(tracePath.lastIndexOf('/')+1);// 文件名
        if (fileName != null) {
            File file = new File(tracePath);
            if (file.exists()) {
                response.setContentType("application/force-download");// 设置强制下载不打开
                response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);
                byte[] buffer = new byte[1024];
                FileInputStream fis = null;
                BufferedInputStream bis = null;
                try {
                    fis = new FileInputStream(file);
                    bis = new BufferedInputStream(fis);
                    OutputStream os = response.getOutputStream();
                    int i = bis.read(buffer);
                    while (i != -1) {
                        os.write(buffer, 0, i);
                        i = bis.read(buffer);
                    }
                    return result;
                } catch (Exception e) {
                    logger.info(e.getMessage());
                } finally {
                    if (bis != null) {
                        try {
                            bis.close();
                        } catch (IOException e) {
                            logger.info(e.getMessage());
                        }
                    }
                    if (fis != null) {
                        try {
                            fis.close();
                        } catch (IOException e) {
                            logger.info(e.getMessage());
                        }
                    }
                }
            }
        }
        result.setCode(ResultCode.FAIL.code());
        result.setMessage("Trace download fialed");
        return result;
    }


}
